package com.sunni.repository.JDBC.Impl;

import com.sunni.entity.JDBC.Student;
import com.sunni.repository.JDBC.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("jdbc_stuRep")
public class StudentRepositoryImpl implements StudentRepository {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Student> findAll() {
        return jdbcTemplate.query("select * from stu",new BeanPropertyRowMapper<>(Student.class));
    }

    @Override
    public Student findById(long id) {
        return jdbcTemplate.queryForObject("select * from stu where id=?",new Object[]{id},new BeanPropertyRowMapper<>(Student.class));
    }

    @Override
    public void save(Student student) {
        jdbcTemplate.update("insert into stu(name,age) values(?,?)",student.getName(),student.getAge());
    }

    @Override
    public void delete(long id) {
        jdbcTemplate.update("delete from stu where id=?",id);
    }

    @Override
    public void update(Student student) {
        jdbcTemplate.update("update `stu` set `name`=?,`age`=? where id=?",student.getName(),student.getAge(),student.getId());
    }
}
